import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
//打包时把下面的引入element-ui的css注释掉可进一步优化打包后体积 不注释则依然会打包200kb的element-ui插件
import 'element-ui/lib/theme-chalk/index.css'
import './assets/global.css'
import 'lib-flexible/flexible'
import axios from 'axios'
import './assets/icon/iconfont.css'
import elTableInfiniteScroll from 'el-table-infinite-scroll'
Vue.use(elTableInfiniteScroll);
Vue.config.productionTip = false
Vue.use(ElementUI, {
	size: 'small'
})
Vue.prototype.$axios = axios;
Vue.prototype.baseURL="http://www.xxx:80xx"    // 接口地址
//重写storage的setItem，当session的key为username时使用 用以监听sessionStorage的变化 实现登陆后首页用户名的改变
Vue.prototype.resetSetItem = function(key, newVal) {
	if (key === "username") {
		var newStorageEvent = document.createEvent('StorageEvent');
		const storage = {
			setItem: function(k, val) {
				sessionStorage.setItem(k, val);
				//初始化创建的事件
				newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
				//派发对象
				window.dispatchEvent(newStorageEvent)
			}
		}
		return storage.setItem(key, newVal);
	}

}
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
axios.defaults.headers.common['Authorization'] = ''
//请求拦截器
axios.interceptors.request.use(function(config) {
	let token = sessionStorage.token
	if (token) {
		config.headers.common['Authorization'] = token;
	}
	return config
}, function(err) {
	return Promise.reject(err)
})
// 全局拦截401状态码并执行登出返回登录页操作
axios.interceptors.response.use(function(res) {
	if (res.data.status == 401) {
		ElementUI.Notification({
			title: '失败',
			message: res.data.msg,
			type: 'error'
		})
		router.push('/login')
	}
	return res
}, function(err) {
	return Promise.reject(err)
})

Vue.prototype.$eventBus = new Vue()
new Vue({
	router,
	render: h => h(App)
}).$mount('#app')
